package com.zhuanghdong.controller;

import com.zhuanghdong.pojo.Headline;
import com.zhuanghdong.service.HeadlineService;
import com.zhuanghdong.utils.JwtHelper;
import com.zhuanghdong.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 头条模块开发
 * 功能分类包括：头条发布、
 *            根据hid查询头条详情、
 *            更新头条信息、
 *            删除
 */
@RestController
@RequestMapping("headline")
@CrossOrigin
public class HeadlineController {
    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private HeadlineService headlineService;

    /**
     * 头条发布
     * @param headline @param token
     * @return
     */
    @PostMapping("publish")
    public Result publish(@RequestBody Headline headline, @RequestHeader String token){
        int userId=jwtHelper.getUserId(token).intValue();
        headline.setPublisher(userId);
        Result result=headlineService.publish(headline);
        return result;
    }

    /**
     * 根据hid查询头条详情
     * @param hid
     * @return
     */
    @PostMapping("findHeadlineByHid")
    public Result findHeadlineByHid(@RequestHeader Integer hid){
        Result result=headlineService.findHeadlineByHid(hid);
        return result;
    }

    /**
     * 更新头条信息
     * @param headline
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody Headline headline){
        Result result=headlineService.updateData(headline);
        return result;
    }

    /**
     * 删除
     * @param hid
     * @return
     */
    @PostMapping("removeByHid")
    public Result removeByHid(Integer hid){
        //调用业务层直接删除
        headlineService.removeById(hid);
        return Result.ok(null);
    }
}
